home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-9.10-netbook-remix-PL.iso / casper / filesystem.squashfs / usr / share / pyshared / ibus / config.py < prev    next >
Text File  |  2009-11-05  |  5KB  |  143 lines

  1. # vim:set et sts=4 sw=4:
  2. #
  3. # ibus - The Input Bus
  4. #
  5. # Copyright (c) 2007-2008 Huang Peng <shawn.p.huang@gmail.com>
  6. #
  7. # This library is free software; you can redistribute it and/or
  8. # modify it under the terms of the GNU Lesser General Public
  9. # License as published by the Free Software Foundation; either
  10. # version 2 of the License, or (at your option) any later version.
  11. #
  12. # This library is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. # GNU Lesser General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU Lesser General Public
  18. # License along with this program; if not, write to the
  19. # Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  20. # Boston, MA  02111-1307  USA
  21.  
  22. __all__ = (
  23.         "ConfigBase",
  24.         "IBUS_SERVICE_CONFIG",
  25.         "IBUS_PATH_CONFIG"
  26.     )
  27.  
  28. IBUS_SERVICE_CONFIG = "org.freedesktop.IBus.Config"
  29. IBUS_PATH_CONFIG = "/org/freedesktop/IBus/Config"
  30.  
  31. import gobject
  32. import object
  33. import interface
  34. import dbus
  35. from dbus.proxies import ProxyObject
  36.  
  37. class ConfigBase(object.Object):
  38.     def __init__(self, bus):
  39.         super(ConfigBase, self).__init__()
  40.         self.__proxy = ConfigProxy(self, bus.get_dbusconn())
  41.  
  42.     def get_value(self, section, name):
  43.         pass
  44.  
  45.     def set_value(self, section, name, value):
  46.         pass
  47.  
  48.     def value_changed(self, section, name, value):
  49.         self.__proxy.ValueChanged(section, name, value)
  50.  
  51.  
  52. class ConfigProxy(interface.IConfig):
  53.     def __init__ (self, config, dbusconn):
  54.         super(ConfigProxy, self).__init__(dbusconn, IBUS_PATH_CONFIG)
  55.         self.__dbusconn = dbusconn
  56.         self.__config = config
  57.  
  58.     def GetValue(self, section, name):
  59.         return self.__config.get_value(section, name)
  60.  
  61.     def SetValue(self, section, name, value):
  62.         return self.__config.set_value(section, name, value)
  63.  
  64.     def Destroy(self):
  65.         self.__config.destroy()
  66.  
  67. class Config(object.Object):
  68.     __gtype_name__ = "PYIBusConfig"
  69.     __gsignals__ = {
  70.         "reloaded" : (
  71.             gobject.SIGNAL_RUN_LAST,
  72.             gobject.TYPE_NONE,
  73.             ()
  74.         ),
  75.         "value-changed" : (
  76.             gobject.SIGNAL_RUN_LAST,
  77.             gobject.TYPE_NONE,
  78.             (gobject.TYPE_STRING, gobject.TYPE_STRING, gobject.TYPE_PYOBJECT)
  79.         ),
  80.     }
  81.  
  82.     def __init__(self, bus):
  83.         super(Config, self).__init__()
  84.         self.__bus = bus
  85.         self.__bus_name = None
  86.  
  87.         self.__bus.add_match("type='signal',\
  88.                               sender='org.freedesktop.DBus',\
  89.                               member='NameOwnerChanged',\
  90.                               arg0='org.freedesktop.IBus.Config'")
  91.         self.__bus.get_dbusconn().add_signal_receiver(self.__name_owner_changed_cb, signal_name="NameOwnerChanged")
  92.  
  93.         try:
  94.             self.__init_config()
  95.         except:
  96.             self.__config = None
  97.  
  98.     def __name_owner_changed_cb(self, name, old_name, new_name):
  99.         if name == "org.freedesktop.IBus.Config":
  100.             if new_name == "":
  101.                 self.__config = None
  102.             else:
  103.                 self.__init_config(new_name)
  104.  
  105.     def __init_config(self, bus_name=None):
  106.         if bus_name == None:
  107.             bus_name = self.__bus.get_name_owner(IBUS_SERVICE_CONFIG)
  108.  
  109.         match_rule = "type='signal',\
  110.                       sender='%s',\
  111.                       member='ValueChanged',\
  112.                       path='/org/freedesktop/IBus/Config'"
  113.  
  114.         if self.__bus_name:
  115.             self.__bus.remove_match(match_rule % self.__bus_name)
  116.             self.__bus_name = None
  117.  
  118.         self.__config = self.__bus.get_dbusconn().get_object(bus_name, IBUS_PATH_CONFIG)
  119.         self.__config.connect_to_signal("ValueChanged", self.__value_changed_cb)
  120.  
  121.         self.__bus_name = bus_name
  122.         self.__bus.add_match(match_rule % self.__bus_name)
  123.         self.emit("reloaded")
  124.  
  125.     def __value_changed_cb(self, section, name, value):
  126.         self.emit("value-changed", section, name, value)
  127.  
  128.     def get_value(self, section, name, default_value):
  129.         try:
  130.             return self.__config.GetValue(section, name)
  131.         except:
  132.             return default_value
  133.  
  134.     def set_value(self, section, name, value):
  135.         try:
  136.             return self.__config.SetValue(section, name, value)
  137.         except:
  138.             return
  139.  
  140.     def set_list(self, section, name, value, signature):
  141.         return self.set_value(section, name, dbus.Array(value, signature=signature))
  142.  
  143.